//
//  ViewController.m
//  112-section
//
//  Created by student on 16/3/3.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSDictionary *name;

@property (nonatomic,strong) NSArray *sortArr;

//搜索控制器
@property (nonatomic,strong) UISearchController *searchController;

//用来存放搜索结果
@property (nonatomic,strong) NSMutableArray *reslutArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames.plist" ofType:nil];
    self.name = [NSDictionary dictionaryWithContentsOfFile:path];
    self.sortArr = [[self.name allKeys] sortedArrayUsingSelector:@selector(compare:)];
    // 创建搜索控制器
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;//处理搜索
    self.tableView.tableHeaderView = self.searchController.searchBar;
}
//多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.searchController.active) {
        return 1;
    }
    else{
        return self.sortArr.count;
    }
}
//每组多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.searchController.active) {
        return self.reslutArr.count;
    }
    else{
        NSString *key = self.sortArr[section];
        NSArray *arr = self.name[key];
        return arr.count;
    }
}
//每行内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ooo"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ooo"];
    }
    if (self.searchController.active) {
        cell.textLabel.text = self.reslutArr[indexPath.row];
        return cell;
    }
    else{
        NSString *key = self.sortArr[indexPath.section];
        NSArray *arr = self.name[key];
        cell.textLabel.text = arr[indexPath.row];
        return cell;
    }
}
//设置段头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.sortArr[section];
}
//设置段尾
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return 0;
}
//右侧导航
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.sortArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 搜索结果的处理事件
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *searchStr = searchController.searchBar.text;
    if (searchStr.length > 0) {
        self.reslutArr = [NSMutableArray array];
        NSPredicate *pre = [NSPredicate predicateWithBlock:^BOOL(NSString *name, NSDictionary<NSString *,id> * _Nullable bindings) {
            NSRange range = [name rangeOfString:searchStr options:NSCaseInsensitiveSearch];
            return range.location != NSNotFound;
        }];
        for (NSString *key in self.sortArr) {
            NSArray *names = self.name[key];
            NSArray *tem = [names filteredArrayUsingPredicate:pre];
            [self.reslutArr addObjectsFromArray:tem];
        }
    }
    [self.tableView reloadData];
}
@end
